<?php
require_once PATH_NAME .'/utilities/sessions.php';
require_once PATH_NAME .'/includes/access.php';
require_once PATH_NAME .'/includes/html.php';
require_once PATH_NAME .'/includes/menu.php';
class userView{
	
	private $lang = '';
	
	public function __construct(){
		$this->lang = $this->getLanguage();
	}
	
	public function getLanguage(){
		$lang = sessions::getSessionByName();
		if (file_exists('language/user_'.$lang.'.php')){
			require_once 'language/user_'.$lang.'.php';
		}else{
			require_once 'language/user_en.php';
		}
		return $lang;		
	}
	
	public function loginForm(){
		?>
		<?php 
		HTML::topHTML('Camlot');
		?>
		<body>
		
		<center>
		<div style="width: 1000px; height: auto;margin-top: 10%">
		<form name="loginForm" action="login.php" method="post">
			<table border="0" cellpadding="4" cellspacing="0" width="500" style="border: 1px solid black;">
			<tbody>
				<tr>
					<td colspan="2" height="50" style="color: yellow;font-size: 25px;font-weight:bold; text-align: center;background-image: url('images/login-t.png');"><?php echo AUTHENTICATION?></td>
				</tr>
				<tr height="100">
					<td colspan="2" style="text-align: center"><?php echo PLEASE_ENTER_USERNAME?></td>
				</tr>
				<tr>
					<td style="width: 200px;"><div style="padding-left: 70px"><?php echo USERNAME?> :</div></td>
					<td><input type="text" style="width: 160px;" name="username" value="" /></td>
				</tr>
				<tr>
					<td><div style="padding-left: 70px"><?php echo PASSWORD?> :</div></td>
					<td><input type="password" style="width: 160px;" name="password" value="" /></td>
				</tr>
				<tr height="20">
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td height="50" colspan="2" style="text-align: center;background-image: url('images/login-b.png')">
						<input style="background-image: url('images/login-button.png');height: 35px;border: 1px solid #CCCCCC;width: 80px;color: white;" type="submit" name="login" value="<?php echo BUTTON_LOGIN ?>" />
						<input style="background-image: url('images/login-button.png');height: 35px;border: 1px solid #CCCCCC;width: 80px;color: white;" type="reset" name="cancel" value="<?php echo BUTTON_CANCEL ?>" />
					</td>			
				</tr>
			</tbody>
			</table>
		</form>
		</div>
		</center>
		</body>
		</html>
		<?php 		
	}
	public function regCustomerForm( &$lists = NULL ){
	?>	
		<?php 
		$user = sessions::getSession();
		if ( empty($user) ){
			Access::setRedirect("index.php");
		}
		HTML::topHTML('Camlot');
		?>
		<script src="<?php echo WROOT?>views/js/query.min.js" language="javascript"></script>
		<script>
		$(function(){
			$("#RegisterForm").submit(function (){
				var nbsubmit = 0;
				var id = $("#id").val();			
				if (id != 0){
					$("#password").removeClass('required');
					$("#rpassword").removeClass('required');				
				}
				$(".required").each(function (){
					nbsubmit += validationField( $(this) );						 
				});
				if (nbsubmit != 0){
					return false;	
				}	
				//return false;
			});
			$('.required').change(function (){
				validationField( $(this) );							
			});
		});
		function validationField(sel){
			var nb = 0;			
			if(sel.val() == ""){
				sel.addClass('field_require');
				sel.parent().parent().addClass('text_require');
				nb++;
			}else{
				sel.removeClass('field_require');
				sel.parent().parent().removeClass('text_require');
			}	
			return nb;
		}
		</script>	
		<?php		
		HTML::wrapperHeaderHTML(); 
		HTML::headerHTML( $user['name'] );		
		?>
		<form name="saveForm" id="RegisterForm" action="usersaction.php" method="POST">
			<div id="userRegistration" style="text-align: left;">
				<fieldset>
				<legend>Personal info</legend>							
				<table border="0" cellpadding="2" cellspacing="0">
				<tbody>
					<tr>
						<td width="250px;">Fullname</td>
						<td><input class="required" type="text" name="fullname" value="<?php echo @$lists['info']['fullname']?>" /></td>
					</tr>	
					<tr>
						<td>Gendor</td>
						<td>
							<select class="required" name="sex" id="sex" size="1" title="Gender">
								<option value="" selected="selected">- Select One -</option>
								<option value="M" <?php echo (@$lists['info']['sex']=='M')?'selected':''; ?> >- Male -</option>
								<option value="F" <?php echo (@$lists['info']['sex']=='F')?'selected':''; ?>>- Female -</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>Date of birth</td>
						<td>
							<select class="required" name="monthdob" id="monthdob" title="- Select Month -">
								<option selected="selected" value="">- Select Month -</option>
								<option value="01" <?php echo (@$lists['info']['monthdob']=='01')?'selected':''; ?>>January</option>
								<option value="02" <?php echo (@$lists['info']['monthdob']=='02')?'selected':''; ?>>February</option>
								<option value="03" <?php echo (@$lists['info']['monthdob']=='03')?'selected':''; ?>>March</option>
								<option value="04" <?php echo (@$lists['info']['monthdob']=='04')?'selected':''; ?>>April</option>
								<option value="05" <?php echo (@$lists['info']['monthdob']=='05')?'selected':''; ?>>May</option>
								<option value="06" <?php echo (@$lists['info']['monthdob']=='06')?'selected':''; ?>>June</option>
								<option value="07" <?php echo (@$lists['info']['monthdob']=='07')?'selected':''; ?>>July</option>
								<option value="08" <?php echo (@$lists['info']['monthdob']=='08')?'selected':''; ?>>August</option>
								<option value="09" <?php echo (@$lists['info']['monthdob']=='09')?'selected':''; ?>>September</option>
								<option value="10" <?php echo (@$lists['info']['monthdob']=='10')?'selected':''; ?>>October</option>
								<option value="11" <?php echo (@$lists['info']['monthdob']=='11')?'selected':''; ?>>November</option>
								<option value="12" <?php echo (@$lists['info']['monthdob']=='12')?'selected':''; ?>>December</option>
							</select>	
							<input class="required" style="width: 50px" type="text" name="daydob" id="daydob" value="<?php echo @$lists['info']['daydob']?>" />
							<input class="required" style="width: 50px"  type="text" name="yeardob" id="yeardob" value="<?php echo @$lists['info']['yeardob']?>" />					
						</td>
					</tr>	
					<tr>
						<td>Telephone</td>
						<td><input class="required" type="text" name="tel" value="<?php echo @$lists['info']['tel']?>" /></td>
					</tr>
					<tr>
						<td valign="top">Address</td>
						<td>
							<textarea class="required" name="address" cols="50" rows="4"><?php echo @$lists['info']['address']?></textarea>							
						</td>
					</tr>
				</tbody>
				</table>
				</fieldset>
				<fieldset>
				<legend>Site info</legend>
				<table border="0" cellpadding="2" cellspacing="0">
				<tbody>
					<tr>
						<td width="250px;">Username</td>
						<td><input class="required" <?php echo @$lists['info'][0]['username'];?> type="text" name="username" value="<?php echo @$lists['info']['username'];?>" /></td>
					</tr>
					<tr>
						<td>Password</td>
						<td><input class="required" id="password" type="password" name="password" value="<?php echo @$lists['info']['password']?>" /></td>
					</tr>
					<tr>
						<td>Retype Password</td>
						<td><input class="required" id="rpassword" type="password" name="retypepassword" value="<?php echo @$lists['info']['password']?>" /></td>
					</tr>
				</tbody>
				</table>
				</fieldset>
				
				<fieldset>
					<legend>Save</legend>
					<div><input type="submit" name="save" value="Save" /></div>
				</fieldset>
                <input type="hidden" name="task" value="saveCustomer">
                <input type="hidden" id="id" name="id" value="<?php echo (@$lists['info']['id'])?@$lists['info']['id']:0;?>">	
			</div>				
		</form>
		<?php 			
		HTML::wrapperFooterHTML();
		menu::mainmenu( $this->lang );
	}
	public function htmlFormRegAgent(){
	?>
		<div id="userRegistration" style="text-align: left;">
			<table border="0" cellpadding="2" cellspacing="0">
			<tbody>
				<tr>
					<td width="140">Agent name</td>
					<td><input class="required" type="text" name="agentname" value="" id="agentname" /></td>
				</tr>
				<tr>
					<td>Telephone</td>
					<td><input class="required" type="text" name="telephone" value="" id="telephone" /></td>
				</tr>
				<tr>
					<td>Email</td>
					<td><input class="required" type="text" name="email" value="" id="email" /></td>
				</tr>
				<tr>
					<td>Address</td>
					<td><textarea name="address" id="address" cols="18" rows="2"></textarea></td>
				</tr>
				<tr>
					<td>Discount2</td>
					<td><input class="required" type="text" name="discount2" value="" id="discount2" /></td>
				</tr>
				<tr>
					<td>Discount3</td>
					<td><input class="required" type="text" name="discount3" value="" id="discount3" /></td>
				</tr>
				<tr>
					<td>Discount4</td>
					<td><input class="required" type="text" name="discount4" value="" id="discount4" /></td>
				</tr>
				<tr>
					<td>Discount5</td>
					<td><input class="required" type="text" name="discount5" value="" id="discount5" /></td>
				</tr>
				<tr>
					<td>Win factor2</td>
					<td><input class="required" type="text" name="win_factor2" value="" id="win_factor2" /></td>
				</tr><tr>
					<td>Win factor3</td>
					<td><input class="required" type="text" name="win_factor3" value="" id="win_factor3" /></td>
				</tr>
				<tr>
					<td>Win factor4</td>
					<td><input class="required" type="text" name="win_factor4" value="" id="win_factor4" /></td>
				</tr>
				<tr>
					<td>Win factor5</td>
					<td><input class="required" type="text" name="win_factor5" value="" id="win_factor5" /></td>
				</tr>
			</tbody>
			</table>
			<input type="hidden" id="idAgent" name="id" value="0">
		</div>
	<?php 
	}
	public function htmlFormRegCustomer( ){
	?>
			<div id="userRegistration" style="text-align: left;">
				<fieldset>
										
				<table border="0" cellpadding="2" cellspacing="0">
				<tbody>
					<tr>
						<td width="100px;">Fullname</td>
						<td><input class="required" type="text" name="fullname" value="" id="fullname" /></td>
					</tr>	
					<tr>
						<td>Gendor</td>
						<td>
							<select class="required" name="sex" id="sex" size="1" title="Gender">
								<option value="" selected="selected">- Select One -</option>
								<option value="M" >- Male -</option>
								<option value="F" >- Female -</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>Date of birth</td>
						<td>
							<select class="required" name="monthdob" id="monthdob" title="- Select Month -">
								<option selected="selected" value="">- Month -</option>
								<option value="01">January</option>
								<option value="02">February</option>
								<option value="03">March</option>
								<option value="04">April</option>
								<option value="05">May</option>
								<option value="06">June</option>
								<option value="07">July</option>
								<option value="08">August</option>
								<option value="09">September</option>
								<option value="10">October</option>
								<option value="11">November</option>
								<option value="12">December</option>
							</select>
							<select class="required" name="daydob" id="daydob" title="- Select Day -">
								<option selected="selected" value="">- Day -</option>
								<?php 
								for( $i = 1; $i<=31;$i++){
									if ($i<10){
										echo "<option value=0".$i.">0".$i."</option>";	
									}else{
										echo "<option value=".$i.">".$i."</option>";	
									}
								}
								?>
							</select>
							<select class="required" name="yeardob" id="yeardob" title="- Select Year -">
								<option selected="selected" value="">- Year -</option>
								<?php 
								for( $j = 2000; $j>=1950;$j--){
								?>
									<option value="<?php echo $j?>"><?php echo $j?></option>
								<?php 	
								}
								?>
							</select>													
						</td>
					</tr>	
					<tr>
						<td>Telephone</td>
						<td><input class="required" type="text" name="tel" value="" id="tel" /></td>
					</tr>
					<tr>
						<td valign="top">Address</td>
						<td>
							<textarea class="required" name="address" id="address" cols="40" rows="4"></textarea>							
						</td>
					</tr>
				</tbody>
				</table>
				
				<table border="0" cellpadding="2" cellspacing="0">
				<tbody>
					<tr>
						<td width="100px;">Username</td>
						<td><input class="required" type="text" name="username" value="" id="username" /></td>
					</tr>
					<tr>
						<td>Password</td>
						<td><input class="required" id="password" type="password" name="password" value="" /></td>
					</tr>
					<tr>
						<td>Retype Password</td>
						<td><input class="required" id="rpassword" type="password" name="retypepassword" value="" /></td>
					</tr>
				</tbody>
				</table>
				</fieldset>				
                <input type="hidden" name="task" value="saveCustomer">
                <input type="hidden" id="idCustomer" name="id" value="0">	
			</div>
	<?php 	
	}
	
	public function regContractForm( $lists = NULL ){
	?>	
		<?php 
		echo $lang;
		$user = sessions::getSession();
		if ( empty($user) ){
			Access::setRedirect("index.php");
		}
		HTML::topHTML( CONTRACT );
		?>
		<script src="<?php echo WROOT?>views/js/query.min.js" language="javascript"></script>
		<script>
		$(function(){
			
		});
		</script>	
		<?php
		HTML::wrapperHeaderHTML(); 
		HTML::headerHTML( $user['name'] );		
		?>
		<form name="saveContract" method="POST" action="usersaction.php" id="contractForm">
			<div id="userRegistration" style="text-align: left;">
				<fieldset>
					<legend><?php echo CONTRACT_INFO?></legend>
					<table border="0" cellpadding="2" cellspacing="0">
					<tbody>
						<tr>
							<td width="250px;"><?php echo CONTRACT_NAME?></td>
							<td><input type="text" name="conName" value="<?php $lists['con']['conName']?>" /></td>
						</tr>
						<tr>
							<td><?php echo CONTRACT_START_DATE?></td>
							<td><?php echo HTML::getCalendar($this->lang, 'conStartDate', $lists['con']['conStartDate'], true);?></td>
						</tr>
						<tr>
							<td><?php echo CONTRACT_END_DATE?></td>
							<td><?php echo HTML::getCalendar($this->lang, 'conEndDate', $lists['con']['conEndDate'], false);?></td>
						</tr>
						<tr>
							<td valign="top"><?php echo CONTRACT_DESCRIPTION?></td>							
							<td>
								<textarea name="description" cols="50" rows="4"><?php $lists['con']['description']?></textarea>							
							</td>
						</tr>
					</tbody>
					</table>
				</fieldset>
				<fieldset>
					<legend>Save</legend>
					<div><input type="submit" name="save" value="Save" /></div>
				</fieldset>
			</div>
			<input type="hidden" name="task" value="saveContract">
		</form>
		<?php 			
		HTML::wrapperFooterHTML();
		menu::mainmenu( $this->lang );
	}
	
	public function showAgentList( &$lists = NULL ){
	?>	
		<?php 
		$user = sessions::getSession();
		if ( empty($user) ){
			Access::setRedirect("index.php");
		}
		HTML::topHTML('Agent');		
		?>
		<link rel="stylesheet" href="<?php echo WROOT?>views/js/framework/themes/base/jquery.ui.all.css">		
		<script src="<?php echo WROOT?>views/js/framework/external/jquery.bgiframe-2.1.2.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.core.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.widget.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.mouse.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.button.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.draggable.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.position.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.resizable.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.dialog.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.effects.core.js"></script>
		<script>
		$(function(){
			// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
			$( "#dialog:ui-dialog" ).dialog( "destroy" );
			
			var agentname = $( "#agentname" ),
				id = $( "#idAgent" ),
				telephone = $("#telephone"),
				email = $( "#email" ),
				address = $( "#address" ),
				discount2 = $("#discount2"),
				discount3 = $("#discount3"),
				discount4 = $("#discount4"),
				discount5 = $("#discount5"),
				win_factor2 = $("#win_factor2"),
				win_factor3 = $("#win_factor3"),
				win_factor4 = $("#win_factor4"),
				win_factor5 = $("#win_factor5"),
				allFields = $( [] ).add( agentname ).add( id ).add( telephone ).add( email ).add( address ).add( discount2 ).add( discount3 ).add( discount4 ).add( discount5 ).add( win_factor2 ).add( win_factor3 ).add( win_factor4 ).add( win_factor5 ),
				tips = $( ".validateTips" );

			function updateTips( t ) {
				tips
					.text( t )
					.addClass( "ui-state-highlight" );
				setTimeout(function() {
					tips.removeClass( "ui-state-highlight", 1500 );
				}, 500 );
			}

			function checkLength( o, n, min, max ) {
				if ( o.val().length > max || o.val().length < min ) {
					o.addClass( "ui-state-error" );
					updateTips( "Length of " + n + " must be between " +
						min + " and " + max + "." );
					return false;
				} else {
					return true;
				}
			}

			function checkRegexp( o, regexp, n ) {
				if ( !( regexp.test( o.val() ) ) ) {
					o.addClass( "ui-state-error" );
					updateTips( n );
					return false;
				} else {
					return true;
				}
			}
			function checkCharacter( sel ){
				if ( sel.val() == "" ){
					sel.addClass( "ui-state-error" );
					sel.focus();
					return false;
				}else{
					return true;
				}
			}
			function checkCharacterHtml( sel ){
				if ( sel.text() == "" ){
					sel.addClass( "ui-state-error" );
					sel.focus();
					return false;
				}else{
					return true;
				}
			}
			
			$( "#dialog-form" ).dialog({
				autoOpen: false,
				height: 455,
				width: 400,
				modal: true,
				buttons: {
					"Add agent": function() {
						var bValid = true;
						allFields.removeClass( "ui-state-error" );
						idAgent = $("#idAgent").val();						
						bValid = bValid && checkLength( agentname, "fullname", 3, 16 );
						bValid = bValid && checkCharacter( telephone );				
						bValid = bValid && checkLength( email, "email", 6, 80 );
						
						//bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
						// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
						bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
						//bValid = bValid && checkRegexp( username, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );												
						if ( bValid ){																					
							var urls = "<?php echo WROOT?>views/users/ajaxAgent.php";							
							msg = $.ajax({
								  url: urls+"?agent=add&id="+id.val()+"&agentname="+agentname.val()+"&telephone="+telephone.val()+"&email="+email.val()+"&address="+address.val()+"&discount2="+discount2.val()+"&discount3="+discount3.val()+"&discount4="+discount4.val()+"&discount5="+discount5.val()+"&win_factor2="+win_factor2.val()+"&win_factor3="+win_factor3.val()+"&win_factor4="+win_factor4.val()+"&win_factor5="+win_factor5.val(),								  
							      global: false,
							      type: "POST",
							      async:false,
							      success: function(msg){							     
						        	 return msg;
							      }
							}).responseText;							
							if (eval(msg) > 0){
								if (id.val() == 0){
									$( "#users tbody" ).append( "<tr>" +
										"<td>" + agentname.val() + "</td>" + 
										"<td>" + telephone.val() + "</td>" + 
										"<td>" + email.val() + "</td>" +
										"<td>" + discount2.val() + "</td>" +
										"<td>" + discount3.val() + "</td>" +
										"<td>" + discount4.val() + "</td>" +
										"<td>" + discount5.val() + "</td>" +
										"<td>" + win_factor2.val() + "</td>" +
										"<td>" + win_factor3.val() + "</td>" +
										"<td>" + win_factor4.val() + "</td>" +
										"<td>" + win_factor5.val() + "</td>" +																		
										"<td><a href='#' onclick='editAgent("+msg+"); return false;'>Edit</a></td>" +
									"</tr>" );									
								}else{																					
									$( "#agent"+id.val() ).each(function (){
										$( this ).find("td:eq(0)").html(agentname.val());
										$( this ).find("td:eq(1)").html(telephone.val());
										$( this ).find("td:eq(2)").html(email.val());
										$( this ).find("td:eq(3)").html(discount2.val());
										$( this ).find("td:eq(4)").html(discount3.val());
										$( this ).find("td:eq(5)").html(discount4.val());
										$( this ).find("td:eq(6)").html(discount5.val());
										$( this ).find("td:eq(7)").html(win_factor2.val());
										$( this ).find("td:eq(8)").html(win_factor3.val());
										$( this ).find("td:eq(9)").html(win_factor4.val());
										$( this ).find("td:eq(10)").html(win_factor5.val());	
									});																
								}								
							}	
							$( "#idAgent" ).val(0);
				        	$( this ).dialog( "close" );						
						}
						
					},
					Cancel: function() {
						$( "#idAgent" ).val(0);
						$( this ).dialog( "close" );
					}
				},
				close: function() {
					$( "#idAgent" ).val(0);
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
			});

			$( "#create-user" )
				.button()
				.click(function() {
					$( "#dialog-form" ).dialog( "open" );
			});
		});
		function editAgent(sel){
			$( "#idAgent" ).val(sel);			
			$( "#dialog-form" ).dialog( "open" );
			var urls = "<?php echo WROOT?>views/users/ajaxAgent.php";
			$.ajax({
			  url: urls+"?agent=getAgent&id="+sel,
		      global: false,
		      type: "POST",
		      async:false,
		      dataType: 'json',
		      success: function(msg){							     
	        	 $("#agentname").val(msg.agent_name);	        	 	        	 
	        	 $("#telephone").val(msg.phone);  
	        	 $("#email").val(msg.email);
	        	 $("#address").val(msg.address);
	        	 $("#discount2").val(msg.discount2digit); 
	        	 $("#discount3").val(msg.discount3digit);	        	 	        	 
	        	 $("#discount4").val(msg.discount4digit);  
	        	 $("#discount5").val(msg.discount5digit);
	        	 $("#win_factor2").val(msg.win_factor2digit);
	        	 $("#win_factor3").val(msg.win_factor3digit); 
	        	 $("#win_factor4").val(msg.win_factor4digit);	        	 	        	 
	        	 $("#win_factor5").val(msg.win_factor5digit);  	     	
		      }
			});  
		}
		</script>	
		<style>
			/*body { font-size: 62.5%; }*/
			label, input { display:block; }
			input.text { margin-bottom:12px; width:95%; padding: .4em; }
			fieldset { padding:0; border:0; margin-top:25px;font-size: 70.5%; }
			h1 { font-size: 1.2em; margin: .6em 0; }
			div#users-contain { width: 99%; margin: 20px 0; }
			div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
			div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
			.ui-dialog .ui-state-error { padding: .3em; }
			.validateTips { border: 1px solid transparent; padding: 0.3em; }
		</style>
		<?php
		//print_r($lists);
		HTML::wrapperHeaderHTML(); 
		HTML::headerHTML( $user['name'] );		
		?>		
		<div id="userRegistration" style="text-align: left;">					
			<div id="users-contain" class="ui-widget">
			<button id="create-user">Add agent</button>	
			<table id="users" class="ui-widget ui-widget-content" width="100%">
			<thead>
				<tr class="ui-widget-header ">
					<th>Name</th>
					<th>Telephone</th>
					<th>Email</th>
					<th>Disc 2</th>
					<th>Disc 3</th>
					<th>Disc 4</th>
					<th>Disc 5</th>
					<th>win 2</th>
					<th>win 3</th>
					<th>win 4</th>
					<th>win 5</th>					
					<th></th>
				</tr>
			</thead>
			<tbody>
			<?php				
				//for($i=0;$i<sizeof($lists);$i++){											
				foreach($lists as $liste){					
				?>
				<tr id="<?php echo 'agent'.$liste->agent_id?>">
					<td><?php echo $liste->agent_name?></td>
					<td><?php echo $liste->phone?></td>
					<td><?php echo $liste->email?></td>
					<td><?php echo $liste->discount2digit?></td>
					<td><?php echo $liste->discount3digit?></td>			
					<td><?php echo $liste->discount4digit?></td>
					<td><?php echo $liste->discount5digit?></td>			
					<td><?php echo $liste->win_factor2digit?></td>
					<td><?php echo $liste->win_factor3digit?></td>			
					<td><?php echo $liste->win_factor4digit?></td>
					<td><?php echo $liste->win_factor5digit?></td>										
					<td><a href="#" onclick="editAgent(<?php echo $liste->agent_id?>); return false;">Edit</a></td>
				</tr>
				<?php				
				}
			?>
			</tbody>
            <tfoot>
            	<tr><td colspan="12" align="right"><?php echo HTML::pagination(sizeof($lists))?></td></tr> 
            </tfoot>
			</table>
			</div>
			<div id="dialog-form" title="Add agent">
				<?php $this->htmlFormRegAgent()?>
			</div>
		</div>
		<?php 			
		HTML::wrapperFooterHTML();
		menu::mainmenu( $this->lang );
	}
	public function showCustomersList( &$lists = NULL ){
	?>	
		<?php 
		$user = sessions::getSession();
		if ( empty($user) ){
			Access::setRedirect("index.php");
		}
		HTML::topHTML('Customer');
		?>
		<link rel="stylesheet" href="<?php echo WROOT?>views/js/framework/themes/base/jquery.ui.all.css">		
		<script src="<?php echo WROOT?>views/js/framework/external/jquery.bgiframe-2.1.2.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.core.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.widget.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.mouse.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.button.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.draggable.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.position.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.resizable.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.dialog.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.effects.core.js"></script>
		<script>
		$(function(){
			// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
			$( "#dialog:ui-dialog" ).dialog( "destroy" );
			
			var name = $( "#fullname" ),
				id = $( "#idCustomer" ),
				username = $("#username"),
				password = $( "#password" ),
				sex = $("#sex"),
				monthdob = $("#monthdob"),
				daydob = $("#daydob"),
				yeardob = $("#yeardob"),
				tel = $("#tel"),
				address = $("#address"),
				allFields = $( [] ).add( name ).add( id ).add( username ).add( password ).add( sex ).add( monthdob ).add( daydob ).add( yeardob ).add( tel ).add( address ),
				tips = $( ".validateTips" );

			function updateTips( t ) {
				tips
					.text( t )
					.addClass( "ui-state-highlight" );
				setTimeout(function() {
					tips.removeClass( "ui-state-highlight", 1500 );
				}, 500 );
			}

			function checkLength( o, n, min, max ) {
				if ( o.val().length > max || o.val().length < min ) {
					o.addClass( "ui-state-error" );
					updateTips( "Length of " + n + " must be between " +
						min + " and " + max + "." );
					return false;
				} else {
					return true;
				}
			}

			function checkRegexp( o, regexp, n ) {
				if ( !( regexp.test( o.val() ) ) ) {
					o.addClass( "ui-state-error" );
					updateTips( n );
					return false;
				} else {
					return true;
				}
			}
			function checkCharacter( sel ){
				if ( sel.val() == "" ){
					sel.addClass( "ui-state-error" );
					sel.focus();
					return false;
				}else{
					return true;
				}
			}
			function checkCharacterHtml( sel ){
				if ( sel.text() == "" ){
					sel.addClass( "ui-state-error" );
					sel.focus();
					return false;
				}else{
					return true;
				}
			}
			
			$( "#dialog-form" ).dialog({
				autoOpen: false,
				height: 400,
				width: 400,
				modal: true,
				buttons: {
					"Create an account": function() {
						var bValid = true;
						allFields.removeClass( "ui-state-error" );
						idCustomer = $("#idCustomer").val();						
						bValid = bValid && checkLength( name, "fullname", 3, 16 );
						bValid = bValid && checkCharacter( sex );
						bValid = bValid && checkCharacter( monthdob );
						bValid = bValid && checkCharacter( daydob );
						bValid = bValid && checkCharacter( yeardob );
						bValid = bValid && checkCharacter( tel );
//						bValid = bValid && checkCharacterHtml( address );
//						bValid = bValid && checkLength( email, "email", 6, 80 );
						
						bValid = bValid && checkLength( username, "username", 3, 16 );
						if (idCustomer == 0){
							bValid = bValid && checkLength( password, "password", 5, 16 );
						}

						bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
						// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
//						bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
						bValid = bValid && checkRegexp( username, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
						if (idCustomer == 0){
							bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
						}
						var dial = $(this);
						if ( bValid ){							
							if (idCustomer == 0){
								var dob = yeardob.val()+"-"+daydob.val()+"-"+monthdob.val();
								var urls = "<?php echo WROOT?>views/users/ajaxUsers.php";
								msg = $.ajax({
									  url: urls+"?customers=add&id="+id.val()+"&fullname="+name.val()+"&sex="+sex.val()+"&dob="+dob+"&tel="+tel.val()+"&address="+address.text()+"&username="+username.val()+"&password="+md5(password.val()),
								      global: false,
								      type: "POST",
								      async:false,
								      success: function(msg){							     
							        	 return msg;
								      }
								}).responseText;
								//alert(msg);
								if (eval(msg) > 0){
									$( "#users tbody" ).append( "<tr>" +
										"<td>" + name.val() + "</td>" + 
										"<td>" + sex.val() + "</td>" + 
										"<td>" + dob + "</td>" +
										"<td>" + tel.val() + "</td>" +
										"<td>" + username.val() + "</td>" +									
										"<td><a href='#' onclick='editCutomer("+msg+") return false;'>Edit</a></td>" +
									"</tr>" );									
								}
							}else{
								var dob = yeardob.val()+"-"+daydob.val()+"-"+monthdob.val();
								var urls = "<?php echo WROOT?>views/users/ajaxUsers.php";
								var setPass = "";
								if ( password.val() != "" ){
									setPass = md5(password.val());
								}
								msg = $.ajax({
									  url: urls+"?customers=add&id="+id.val()+"&fullname="+name.val()+"&sex="+sex.val()+"&dob="+dob+"&tel="+tel.val()+"&address="+address.text()+"&password="+setPass,
								      global: false,
								      type: "POST",
								      async:false,
								      success: function(msg){							     
							        	 return msg;
								      }
								}).responseText;								
								if (eval(msg) > 0){									
									$( "#customer"+idCustomer).each(function (){
										$( this ).find("td:eq(0)").html(name.val());
										$( this ).find("td:eq(1)").html(sex.val());
										$( this ).find("td:eq(2)").html(dob);
										$( this ).find("td:eq(3)").html(tel.val());	
										//$( this ).find("td:eq(5)").html(tel.val());	
									});									
								}
							}
							$( "#idCustomer" ).val(0);
				        	$( this ).dialog( "close" );
						}
						
					},
					Cancel: function() {
						$( "#idCustomer" ).val(0);
						$( this ).dialog( "close" );
					}
				},
				close: function() {
					$( "#idCustomer" ).val(0);
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
			});

			$( "#create-user" )
				.button()
				.click(function() {
					$( "#dialog-form" ).dialog( "open" );
			});
			
		});
		function editCutomer(sel){
			$( "#idCustomer" ).val(sel);			
			$( "#dialog-form" ).dialog( "open" );
			var urls = "<?php echo WROOT?>views/users/ajaxUsers.php";
			$.ajax({
			  url: urls+"?customers=getCustomer&id="+sel,
		      global: false,
		      type: "POST",
		      async:false,
		      dataType: 'json',
		      success: function(msg){							     
	        	 $("#fullname").val(msg.name);	        	 
	        	 $("#sex option[value='"+msg.gendor+"']").attr("selected", "selected");
	        	 var dob = msg.birthdate.split("-");    	 
	        	 $("#monthdob option[value='"+dob[1]+"']").attr("selected", "selected");
	        	 $("#daydob option[value='"+dob[2]+"']").attr("selected", "selected");
	        	 $("#yeardob option[value='"+dob[0]+"']").attr("selected", "selected"); 
	        	 $("#tel").val(msg.phone_number);  
	        	 $("#address").val(msg.address);
	        	 $("#username").val(msg.username);
	        	 $("#username").attr("disabled",'disabled');      	 
		      }
			});  
		}
		</script>
		<style>
			/*body { font-size: 62.5%; }*/
			label, input { display:block; }
			input.text { margin-bottom:12px; width:95%; padding: .4em; }
			fieldset { padding:0; border:0; margin-top:25px;font-size: 70.5%; }
			h1 { font-size: 1.2em; margin: .6em 0; }
			div#users-contain { width: 99%; margin: 20px 0; }
			div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
			div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
			.ui-dialog .ui-state-error { padding: .3em; }
			.validateTips { border: 1px solid transparent; padding: 0.3em; }
		</style>	
		<?php
		//print_r($lists);
		HTML::wrapperHeaderHTML(); 
		HTML::headerHTML( $user['name'] );	
		?>		
		<div id="userRegistration" style="text-align: left;">					
			<div id="users-contain" class="ui-widget">
			<P><button id="create-user">Create new user</button></P>	
			<table id="users" class="ui-widget ui-widget-content" width="100%">
			<thead>
				<tr class="ui-widget-header ">
					<th>Name</th>
					<th>Gendor</th>
					<th>Birthday</th>
					<th>Telephone</th>
					<th>Username</th>
					<!--<th>Publish</th>-->
					<th></th>
				</tr>
			</thead>
			<tbody>
			<?php				
				//for($i=0;$i<sizeof($lists);$i++){
				foreach($lists as $list){
				?>
				<tr id="<?php echo 'customer'.$list->id?>">
					<td><?php echo $list->name?></td>
					<td><?php echo $list->gendor?></td>
					<td><?php echo $list->birthdate?></td>
					<td><?php echo $list->phone_number?></td>
					<td><?php echo $list->username?></td>
					<!--<td><?php echo $list->publish?></td>-->
					<!--<td><a href="users.php?edit=customer&id=<?php echo $list->id?>">Edit</a></td>
					--><td><a href="#" onclick="editCutomer(<?php echo $list->id?>); return false;">Edit</a></td>
				</tr>
				<?php
				}
			?>
			</tbody>
            <tfoot>
            	<tr><td colspan="7" align="right"><?php echo HTML::pagination(sizeof($lists))?></td></tr> 
            </tfoot>
			</table>
			</div>
			<div id="dialog-form" title="Create new user">
				<?php $this->htmlFormRegCustomer()?>
			</div>
		</div>
		<?php 			
		HTML::wrapperFooterHTML();
		menu::mainmenu( $this->lang );
	}
	public function showContractList( $lists = NULL ){
	?>	
		<?php 
		$user = sessions::getSession();
		if ( empty($user) ){
			Access::setRedirect("index.php");
		}
		HTML::topHTML('Camlot');
		?>
		<script src="views/js/query.min.js" language="javascript"></script>
		<script>
		$(function(){
			
		});
		</script>	
		<?php
		HTML::wrapperHeaderHTML(); 
		HTML::headerHTML( $user['name'] );		
		?>
		<div id="userRegistration" style="text-align: left;">
			<table border="1" cellpadding="1" cellspacing="1" width="100%">
			<thead>
				<tr>
					<td>Name</td>
					<td>Start Date</td>
					<td>End Date</td>
					<td>Publish</td>
					<td></td>
				</tr>
			</thead>
			<tbody>
			<?php				
				//for($i=0;$i<sizeof($lists);$i++){
				foreach($lists as $list){
				?>
				<tr>
					<td><?php echo $list->con_name?></td>
					<td><?php echo $list->con_datestart?></td>
					<td><?php echo $list->con_dateend?></td>
   					<td><?php echo $list->publish?></td>
					<td></td>
				</tr>
				<?php
				}
			?>
			</tbody>
            <tfoot>
            	<tr><td colspan="5" align="right"><?php echo HTML::pagination(sizeof($lists))?></td></tr> 
            </tfoot>
			</table>
		</div>
		<?php 	
		HTML::wrapperFooterHTML();
		menu::mainmenu( $this->lang );
	}
	public function partenForm( $lists = NULL ){
	?>	
		<?php 
		$user = sessions::getSession();
		if ( empty($user) ){
			Access::setRedirect("index.php");
		}
		HTML::topHTML('Camlot');
		?>
		<script src="views/js/query.min.js" language="javascript"></script>
		<script>
		$(function(){
			
		});
		</script>	
		<?php
		HTML::wrapperHeaderHTML(); 
		HTML::headerHTML( $user['name'] );		
		?>
<!--		 HTML Here-->
		<?php 			
		HTML::wrapperFooterHTML();
		menu::mainmenu( $this->lang );
	}
}
?>